package com.oss.jsoup;

import com.eova.common.utils.time.DateUtil;
import com.jfinal.kit.LogKit;
import com.oss.model.YxmOrder;
import com.oss.model.YxmUser;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class FEmsTT {

    public static void getYxmOrder(String cookie, String startDate, String endDate, String type) {

        System.out.println("startDate=================== "+startDate );
        System.out.println("endDate=================== "+endDate );
        //根据下单时间删除已存在邮件
        YxmOrder.dao.deleteByDate(startDate,endDate,type);

        int pageNo = 1;
        int pageSize = 200;
        int cmt=1;
        while(pageNo<=cmt) {
            String url = "http://10.1.149.227:9500/encourageBack/newOrderAnalysisAction/list";
            //获取请求连接
            Connection con = Jsoup.connect(url);
            //请求头设置，特别是cookie设置
            con.header("Accept", "text/html, */*; q=0.01");
            con.header("Accept-Encoding", "gzip, deflate");
            con.header("Accept-Language", "zh-CN,zh;q=0.9");
            con.header("X-Requested-With", "XMLHttpRequest");
            con.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            con.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");

            con.header("Cookie", cookie);

            con.data("pageNo", pageNo+"");
            con.data("pageSize", pageSize+"");

            con.data("orderNo", "");
            con.data("mailNo",  "");
            con.data("openid", "");
            con.data("userName", "");
            con.data("userCode", "");
            con.data("orgName", "");
            con.data("orgCode", "");
            con.data("createTime", startDate+" 00:00:00 - "+endDate+" 00:00:00");
            con.data("transactionTime", "");

            //解析请求结果
            Document doc=null;
            try {
                doc = con.timeout(300000).ignoreContentType(true).validateTLSCertificates(false).post();
            } catch (IOException e1) {
                System.out.print(e1.getMessage());
                e1.printStackTrace();
            }
            String pageStr = doc.select("span").last().text();
            LogKit.info("pageStr= "+ pageStr);
            int pageCmt = Integer.parseInt(pageStr.substring(pageStr.indexOf("共")+1, pageStr.indexOf("条")));
            cmt = (pageCmt+pageSize-1)/pageSize;
            pageNo++;
            LogKit.info("cmt= "+cmt +" pageStr");

            Element table = doc.getElementById("table_Sort");
            Element tbody = table.selectFirst("tbody");
            Elements tres = tbody.select("tr");

            for(Element ftr:tres){

                try {
                    Elements tdes = ftr.select("td");
                    String orderTime = tdes.get(0).text();
                    String orderId = tdes.get(1).text();
                    String td2 = tdes.get(2).text();
                    String td3 = tdes.get(3).text();
                    String td4 = tdes.get(4).text();
                    String td5 = tdes.get(5).text();
                    String td6 = tdes.get(6).text();


                    LogKit.info("orderTime= "+orderTime);
                    LogKit.info("orderId= "+orderId);

                    String[] td2s = td2.split(" ");
                    String proType = td2s[0].substring(td2s[0].indexOf("：")+1,td2s[0].length());
                    String product = td2s[2];
                    String money = td2s[3].substring(td2s[3].indexOf(":")+1,td2s[3].length()-1);
                    String dealTime = td2s[4].substring(td2s[4].indexOf("：")+1,td2s[4].length());
                    String awb = "";
                    String state = "";
                    String way = "";
                    if("--".equalsIgnoreCase(dealTime)){
                        awb = td2s[5].substring(td2s[5].indexOf("：")+1,td2s[5].length());
                        state = td2s[6].substring(td2s[6].indexOf("：")+1,td2s[6].length());
                        way = td2s[7].substring(td2s[7].indexOf("：")+1,td2s[7].length());
                    }else{
                        dealTime = dealTime+" "+td2s[5];
                        awb = td2s[6].substring(td2s[6].indexOf("：")+1,td2s[6].length());
                        state = td2s[7].substring(td2s[7].indexOf("：")+1,td2s[7].length());
                        way = td2s[8].substring(td2s[8].indexOf("：")+1,td2s[8].length());
                    }

                    String[] td3s = td3.split(" ");
                    String usertel = td3s[0].substring(td3s[0].indexOf("：")+1,td3s[0].length());
                    String userId = td3s[1].substring(td3s[1].indexOf("：")+1,td3s[1].length());
                    String userType = td3s[2].substring(td3s[2].indexOf("：")+1,td3s[2].length());

                    String[] td4s = td4.split(" ");
                    String emp = td4s[0].substring(td4s[0].indexOf("：")+1,td4s[0].length());
                    String empNum = td4s[1].substring(td4s[1].indexOf("：")+1,td4s[1].length());
                    String empTel = td4s[2].substring(td4s[2].indexOf("：")+1,td4s[2].length());
                    String orgName = td4s[3].substring(td4s[3].indexOf("：")+1,td4s[3].length());
                    String orgCode = td4s[4].substring(td4s[4].indexOf("：")+1,td4s[4].length());
                    String orgAddress = td4s[5].substring(td4s[5].indexOf("：")+1,td4s[5].length());

                    String[] td5s = td5.split("：");
                    String crtState = td5s[1].replace("待核销时间","").replace(" ","");
                    String noTime = td5s[2].replace("已核销时间","").replace(" ","");
                    String yesTime = td5s[3].replace("核销失败原因","").replace(" ","");
                    String noReason = td5s[4].replace(" ","");

                    String[] td6s = td6.split(" ");
                    String channel = td6s[0].substring(td6s[0].indexOf("：")+1,td6s[0].length());
                    String activity = td6s[1].substring(td6s[1].indexOf("：")+1,td6s[1].length());

                    YxmOrder yo = new YxmOrder();
                    yo.set("order_time", orderTime).set("order_id",orderId).set("pro_type",proType)
                            .set("product",product).set("money",money).set("deal_time",dealTime).set("awb",awb)
                            .set("state",state).set("way",way).set("user_tel",usertel).set("user_id",userId).set("user_type",userType)
                            .set("emp",emp).set("emp_num",empNum).set("emp_tel",empTel).set("org_name",orgName).set("org_code",orgCode)
                            .set("org_address",orgAddress).set("crt_state",crtState).set("no_time",noTime).set("yes_time",yesTime)
                            .set("no_reason",noReason).set("channel",channel).set("activity",activity).set("type", type).save();
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
            }
        }
    }


    public static void getYxmUser(String cookie, String startDate, String endDate, String type) {

        System.out.println("startDate=================== "+startDate );
        System.out.println("endDate=================== "+endDate );
        YxmUser.dao.deleteByDate(startDate, endDate, type);

        int pageNo = 1;
        int pageSize = 200;
        int cmt=1;
        while(pageNo<=cmt) {
            String url = "http://10.1.149.227:9500/encourageBack/userAnalyse/list";
            //获取请求连接
            Connection con = Jsoup.connect(url);
            //请求头设置，特别是cookie设置
            con.header("Accept", "text/html, */*; q=0.01");
            con.header("Accept-Encoding", "gzip, deflate");
            con.header("Accept-Language", "zh-CN,zh;q=0.9");
            con.header("X-Requested-With", "XMLHttpRequest");
            con.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            con.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
            con.header("Cookie", cookie);
            con.data("pageNo", pageNo+"");
            con.data("pageSize", pageSize+"");
            con.data("openId", "");
            con.data("focusUserName",  "");
            con.data("userName", "");
            con.data("userCode", "");
            con.data("userCode", "");
            con.data("confirmStatus", "");
            con.data("orgName", "");
            con.data("orgCodes", "");
            con.data("channel", "");
            con.data("eventType", "");
            con.data("subscribeTime", startDate+" 00:00:00 - "+endDate+" 00:00:00");

            //解析请求结果
            Document doc=null;
            try {
                doc = con.timeout(300000).ignoreContentType(true).validateTLSCertificates(false).post();
            } catch (IOException e1) {
                System.out.print(e1.getMessage());
                e1.printStackTrace();
            }
            String pageStr = doc.select("span").last().text();
            LogKit.info("pageStr= "+ pageStr);
            int pageCmt = Integer.parseInt(pageStr.substring(pageStr.indexOf("共")+1, pageStr.indexOf("条")));
            cmt = (pageCmt+pageSize-1)/pageSize;
            pageNo++;
            LogKit.info("cmt= "+cmt +" pageStr");

            Element table = doc.getElementById("table_Sort");
            Element tbody = table.selectFirst("tbody");
            Elements tres = tbody.select("tr");
            for(Element ftr:tres){

                try {
                    Elements tdes = ftr.select("td");
                    String orderTime = tdes.get(0).text();
                    String userid = tdes.get(1).text();
                    String username = tdes.get(2).text();
                    String td3 = tdes.get(3).text();
                    String td4 = tdes.get(4).text();
                    String td5 = tdes.get(5).text();
                    String td6 = tdes.get(6).text();


                    String[] td3s = td3.split(" ");
                    String usertel = td3s[0].substring(td3s[0].indexOf("：")+1,td3s[0].length());
                    String useropenId = td3s[1].substring(td3s[1].indexOf("：")+1,td3s[1].length());
                    String bdTime = td3s[2].substring(td3s[2].indexOf("：")+1,td3s[2].length());
                    String jbTime = "";
                    String userEvent ="";

                    if("--".equals(bdTime)){
                        jbTime = td3s[3].substring(td3s[3].indexOf("：")+1,td3s[3].length());
                        if("--".equals(jbTime)){
                            userEvent =  td3s[4].substring(td3s[4].indexOf("：")+1,td3s[4].length());
                        }else{
                            jbTime = jbTime+" "+td3s[4];
                            userEvent = td3s[5].substring(td3s[5].indexOf("：")+1,td3s[5].length());
                        }
                    }else{
                        bdTime = bdTime+" "+td3s[3];
                        jbTime = td3s[4].substring(td3s[4].indexOf("：")+1,td3s[4].length());
                        if("--".equals(jbTime)){
                            userEvent =  td3s[6].substring(td3s[6].indexOf("：")+1,td3s[6].length());
                        }else{
                            jbTime = jbTime+" "+td3s[5];
                            userEvent = td3s[6].substring(td3s[6].indexOf("：")+1,td3s[6].length());
                        }
                    }

                    String[] td4s = td4.split(" ");
                    String emp = td4s[0].substring(td4s[0].indexOf("：")+1,td4s[0].length());
                    String empNum = td4s[1].substring(td4s[1].indexOf("：")+1,td4s[1].length());
                    String empTel = td4s[2].substring(td4s[2].indexOf("：")+1,td4s[2].length());
                    String orgName = td4s[3].substring(td4s[3].indexOf("：")+1,td4s[3].length());
                    String orgCode = td4s[4].substring(td4s[4].indexOf("：")+1,td4s[4].length());
                    String orgAddress = td4s[5].substring(td4s[5].indexOf("：")+1,td4s[5].length());

                    String[] td5s = td5.split("：");
                    String crtState = td5s[1].replace("待核销时间","").replace(" ","");
                    String noTime = td5s[2].replace("已核销时间","").replace(" ","");
                    String yesTime = td5s[3].replace("核销失败原因","").replace(" ","");
                    String noReason = td5s[4].replace(" ","");

                    String[] td6s = td6.split(" ");
                    String channel = td6s[0].substring(td6s[0].indexOf("：")+1,td6s[0].length());
                    String activity = td6s[1].substring(td6s[1].indexOf("：")+1,td6s[1].length());

                    YxmUser yu = new YxmUser();
                    yu.set("order_time",orderTime).set("userid",userid)
//                            .set("username",username)
                            .set("user_tel",usertel).set("user_openid",useropenId)
                            .set("bd_time",bdTime).set("jb_time",jbTime).set("user_event",userEvent).set("emp",emp).set("emp_num",empNum).set("emp_tel",empTel)
                            .set("org_name",orgName).set("org_code",orgCode).set("org_address",orgAddress).set("crt_state",crtState).set("no_time",noTime)
                            .set("yes_time",yesTime).set("no_reason",noReason).set("channel",channel).set("activity",activity).set("type",type).save();
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
            }
        }
    }


    public static void main(String[] args){

        String cookie = "JSESSIONID=A667ACD9CCBE2CE1A38CB9105B92B16A; COOKIE_MEMBER_LOGIN_NAME=%7B%22password%22%3A%22%22%2C%22memoryPassword%22%3A%220%22%2C%22userId%22%3A%22guyw%22%7D";
        String crtDate = DateUtil.getCurrDateStr();
        String begDate = DateUtil.getNextDate(crtDate, -2);

        //FEmsTT.getYxmOrder(cookie,begDate,crtDate,"");
        FEmsTT.getYxmUser(cookie,begDate,crtDate,"");


    }
}
